import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'
import Layout2 from '@/layout/layout2.vue'
import login from '@/views/login.vue'
import register from '@/views/register.vue'

const routes = [
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/register',
    name: 'register',
    component: register
  },
  {
    path: '/',
    component: Layout2,
    redirect: '/task',
    children: [
      {
        path: 'task',
        name: 'Task',
        component: () => import('@/views/task/index.vue'),
        meta: { title: '我的任务' }
      },
      // {
      //   path: 'profile',
      //   name: 'Profile',
      //   component: () => import('@/views/profile/index.vue'),
      //   meta: { title: '个人中心' }
      // },
      // {
      //   path: 'settings',
      //   name: 'Settings',
      //   component: () => import('@/views/settings/index.vue'),
      //   meta: { title: '账号设置' }
      // }
    ]
  },
  // 添加一个直接的任务路由
  {
    path: '/task',
    redirect: '/'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  console.log('路由跳转:', { to, from })
  
  // 检查是否需要登录权限
  // if (to.path === '/login' || to.path === '/register') {
  //   next()
  // } else {
  //   // 检查是否登录
  //   const token = localStorage.getItem('token')
  //   console.log('当前token:', token)
    
  //   if (!token) {
  //     console.log('无token，跳转到登录页')
  //     next('/login')
  //   } else {
  //     console.log('有token，允许访问')
  //     next()
  //   }
  // }
  next()
})

export default router 